/** 
 * Search through RL.  Calls search for tags and for name
 *  type: 1 -> search tags ; 2 -> search name ; 3/null -> search both
 * 
 */ 
function searchReadingList(params, type){
    /* Just to make sure a search type is given */
    if (type == null) type = 3;
    
    /* call |searchRLTags| */
    if (type & 1) return searchRLTags(params);
    
    /* call |searchRLName| */
    if (type & 2) return searchRLName(params);
    
    
}
/** 
 * Search through RL, returns list of items with search params in tags
 * 
 */ 
function searchRLTags(params){
//    var readingList = getReadingList();
    var paramsList = params.toLowerCase().split(" ");
    var retList = [];
    var count = 0; /* Index of |retList|, incremented every time something is added */
    var br = false; /* if !false, it means a search param was met */
    chrome.storage.sync.get(null, function(readingList){
	for (var i in readingList){
	    br = false;
	    var tagList = readingList[i].tags.toLowerCase();
	    for (var j in tagList){
		for (var k in paramsList){
		    if (tagList[j] === paramsList[k]){
			//PRINT |readingList[i]| TO DISPLAY.HTML
			br = !br;
			break;
		    }
		    else continue;
		}
		if (br) break;
		else continue;
	    }
	}
    });
}



/**
 * Search through RL, returns list of items with search params in name
 * 
 */
function searchRLName(params){
    
//    var readingList = getReadingList();
    var paramsList = params.toLowerCase().split(" ");
    var retList = [];
    var count = 0; /* Index of |retList|, incremented every time something is added */
    var br = false; /* if !false, it means a search param was met */
  
    chrome.storage.sync.get({}, function(readingList){  
	for (var i in readingList){
	    br = false;
	    var nameList = readingList[i].title.toLowerCase().split(" ");
	    for (var j in nameList){
		for (var k in paramsList){
		    if (nameList[j] === paramsList[k]){
			//PRINT |readingList[i]| TO DISLPLAY.HTML
			br = !br;
			break;
		    }
		    else continue;
		}
		if (br) break;
		else continue;
	    }
	}
    });
}
